package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.util.ArrayList;

/**
 * 学生数据操作类 使用集合代替数组
 */
public class OtherStudentDao implements BaseStudentDao{


    //创建数组
    private static ArrayList<Student> stus = new ArrayList<>();

    static{
        //初始化数据
        Student student1 = new Student("0001", "鹿晗", 28, "1992");
        Student student2 = new Student("0002", "吴亦凡", 30, "1990");
        Student student3 = new Student("0003", "蔡徐坤", 26, "1994");
        Student student4 = new Student("0004", "黄子韬", 25, "1995");
        stus.add(student1);
        stus.add(student2);
        stus.add(student3);
        stus.add(student4);

    }

    //实现添加学生操作
    public boolean addStudent(Student student) {
        stus.add(student);
        return  true;
    }

    public Student[] findAllStudents() {
        //把集合转变成数组
        //动态初始化数组
        Student[] students= new Student[stus.size()];
        //遍历集合放数据
        for (int i = 0; i < stus.size(); i++) {
            //stus.get(i)  students[i]
            students[i]=stus.get(i);

        }
        return students;
    }

    //删除
    public void deleteStudent(String deleteId) {
        //通过学号查询数组中的位置
        int index = getIndex(deleteId);
        stus.remove(index);

    }

    //查询学号所在的位置
    public int getIndex(String deleteId) {
        for (int i = 0; i < stus.size(); i++) {
            //stus.get(i)
            Student student = stus.get(i);
            if (student!=null&&deleteId.equals(student.getSid())) {
                return i;
            }
        }
        return -1;
    }

    public void updateStudent(String updateId, Student student) {
        int index = getIndex(updateId);
        stus.set(index,student);

    }

    public void deleteStudentByIndex(int index) {
        stus.remove(index);

    }
}
